﻿$(function () {
    //======================
    //======设置查询条件========
    //======================
    //计划年份
    $("#bgjhgyz_toolbar [name='jhnf']").numberspinner({
        min: 2000,
        max: 2100,
        editable: true
    });
    //计划月份
    $("#bgjhgyz_toolbar [name='jhyf']").numberspinner({
        min: 1,
        max: 12,
        editable: true
    });
    //计划单位
    $("#bgjhgyz_toolbar [name='jhdwId']").ljsxyCombobox({lx: 3, sxy: "xy", jyjg: $("#bgjhgyz_toolbar [name='zgsId']").val()});
    //搜索按钮
    $("#bgjhgyz_toolbar [name='searchButton']").click(function () {
        reloadBgjhgyzGrid();
    });
    //======================
    //========操作按钮========
    //======================
    //供应站处理
    $("#bgjhgyz_toolbar [name='cl']").click(function () {
        var row = $('#bgjhgyz_grid').datagrid("getSelected");
        if (row) {
            if (row.wclsl != 0)
                showBgjhgyzDialog("供应站处理", $.webPath() + "/jhgl/bgjhgyz/bgjhgyz_content?id=" + row.id, $.webPath() + "/jhgl/bgjhgyz/save");
            else
                $.messager.alert("提示", "该保供物资计划已经处理!", "warning");
        }
        else
            $.messager.alert("提示", "请选择先选择保供物资计划!", "warning");

    });

//======================
//========计划列表========
//======================
    var grid = $('#bgjhgyz_grid').datagrid({
        url: $.webPath() + '/jhgl/bgjhgyz/bgjhgyzList',
        toolbar: "#bgjhgyz_toolbar",
        pagination: true,
        pageSize: 50,
        rownumbers: true,
        singleSelect: true,
        collapsible: true,
        pageNumber: 1,
        fit: true,
        fitColumns: true,
        queryParams: {
            jhnf: $("#bgjhgyz_toolbar [name='jhnf']").val(),
            jhyf: $("#bgjhgyz_toolbar [name='jhyf']").val()
        },
        columns: [
            [
                {field: 'jhdw', title: '计划单位',align: 'center', width: $(this).width() * 0.1},
                {field: 'sm', title: '计划说明',align: 'center', width: $(this).width() * 0.2},
                {field: 'jhny', title: '计划年月',align: 'center', width: $(this).width() * 0.1},
                {field: 'gyz', title: '供应站',align: 'center', width: $(this).width() * 0.1},
                {field: 'zdr', title: '计划员',align: 'center', width: $(this).width() * 0.1},
                {field: 'wclsl', title: '供应站未处理数量',align: 'right', width: $(this).width() * 0.1},
                {field: 'zt', title: '状态',align: 'center', width: $(this).width() * 0.1,
                    formatter: function (value, row, index) {
                        if (row.hz == 0) {
                            switch (row.zt) {
                                case 1:
                                    return "未提交";
                                case 2:
                                    return "已提交";
                                case 3:
                                    return "已汇总";
                            }
                        }
                        else {
                            switch (row.zt) {
                                case 1:
                                    return "已汇总";
                                case 2:
                                    return "已提交供应站";
                                case 3:
                                    return "供应站已处理";
                                case 4:
                                    return "业务科已处理";
                                case 5:
                                    return "正在提交";
                                case 6:
                                    return "已经提交至物供";
                                case 7:
                                    return "计划科已处理";
                                case 8:
                                    return "采购单已办理";
                            }
                        }
                        return value;
                    }
                }
            ]
        ],
        onLoadSuccess: function (target) {
            var data = $(this).datagrid('getData');
            if (data.length != 0) {
                $(this).datagrid('selectRow', 0);
                checkRowThing();
            }
            $(".datagrid-htable .datagrid-cell").css('text-align', 'center');
        },
        checkRow: function (rowIndex, rowData) {
            checkRowThing();
        },
        onClickRow: function (rowIndex, rowData) {
            checkRowThing();
        }
    });

    //选中一行触发的事件
    function checkRowThing() {
        var tabs = $("#bgjhgyz_tabs").tabs("tabs");
        var row = $('#bgjhgyz_grid').datagrid("getSelected");

        if (row) {
//            var bgjjhgyzmxTab = $("#bgjhgyz_tabs").tabs("getTab", 0);
//            bgjjhgyzmxTab.panel({href:$.webPath() + "/jhgl/bgjhgyz/bgjhgyzmx?id=" + row.id + "&&typeId=3"});
            $('#bgjhgyz_wzjh_grid').datagrid({
                pageNumber: 1,
                url: $.webPath() + "/jhgl/bgjhgyz/bgjhgyzdetail?typeId=3&id=" + row.id
            })
        }
//        var col = [
//            {title: '物资编码', field: 'wzgg.oldcode', width: 100},
//            {title: '物资编码(新)', field: 'wzgg.code', width: 100},
//            {title: '物资名称', field: 'wzgg.name', width: 90},
//            {title: '型号规格', field: 'wzgg.ggxh', width: 90},
//            {title: '单位', field: 'wzgg.dw', width: 60},
//            {title: '计划数量', field: 'jhsl',
//                formatter:function(value){
//                    var sl = formatSl(value,",");
//                    return sl;
//                },width: 80},
//            {title: '库存数量', field: 'kcsj',
//                formatter:function(value){
//                    var sl = formatSl(value,",");
//                    return sl;
//                },width: 80},
//            {title: '一级平衡数量', field: 'sl1',
//                formatter:function(value){
//                    var sl = formatSl(value,",");
//                    return sl;
//                },width: 80},
//            {title: '二级平衡数量', field: 'sl2',
//                formatter:function(value){
//                    var sl = formatSl(value,",");
//                    return sl;
//                },width: 80},
//            {title: '上报数量', field: 'sbsl',
//                formatter:function(value){
//                    var sl = formatSl(value,",");
//                    return sl;
//                },width: 80},
//            {title: '到货日期', field: 'dhrq', width: 90},
//            {title: '设备材料', field: 'sbcl', formatter: function (value) {
//                if (value == 'sb') {
//                    return "设备";
//                }
//                if (value == "cl") {
//                    return "材料";
//                }
//            }, width: 90},
//            {title: '备注', field: 'bz', width: 90},
//            {title: '计划员', field: 'jhy', width: 70},
//            {title: '提交人', field: 'tjr', width: 70},
//            {title: '提交时间', field: 'tjsj', width: 70},
//            {title: '汇总人', field: 'hzr', width: 70},
//            {title: '汇总时间', field: 'hzsj', width: 70},
//            {title: '供应站处理人', field: 'gyzclr', width: 80},
//            {title: '供应站处理时间', field: 'gyzclsj', width: 90},
//            {title: '业务科处理人', field: 'ywkclr', width: 80},
//            {title: '业务科处理时间', field: 'ywkclsj', width: 90},
//            {title: '状态', field: 'zt', width: 90}
//
//        ];
//        if (row) {
//            $("#bgjhgyz_wzjh_grid").datagrid({
//                rownumbers: "true",
//                singleSelect: "true",
//                showFooter: true,
//                fitColumns: true,
//                width: 1800,
//                url: $.webPath() + "/jhgl/bgjhlr/findWzjhhzmxByBgjhId?typeId=3&id=" + row.id,
//                columns: [col]
//            });
//        }

        // 采购合同
//        var col = [
//            {title: '物资编码', field: 'wzgg.oldcode', width: 150},
////            {title: '物资编码(新)', field: 'wzgg.code', width: 150},
//            {title: '物资名称', field: 'wzgg.name', width: 150},
//            {title: '型号规格', field: 'wzgg.ggxh', width: 150},
//            {title: '合同号', field: 'hth', width: 200},
//            {title: '合同签订人', field: 'qdr', width: 150},
//            {title: '合同签订日期', field: 'qdrq', width: 150},
//            {title: '审批人', field: 'spr', width: 150},
//            {title: '审批日期', field: 'sprq', width: 150},
//            {title: '采购方式', field: 'cgfs', width: 80},
//            {title: '备注', field: 'bz', width: 150}
//        ];
//        if (row) {
////            $("#bgjhgyz_cght_grid").datagrid({
////                rownumbers: "true",
////                singleSelect: "true",
////                showFooter: true,
////                fitColumns: true,
////                url: $.webPath() + "/jhgl/bgjhlr/findHtxxByBgjhId?typeId=3&id=" + row.id,
////                columns: [col]
////            });
//            $('#bgjhgyz_cght_grid').datagrid({
//                pageNumber:1,
//                url: $.webPath() + "/jhgl/bgjhlr/bgjhlrcghtdetail?typeId=3&id=" + row.id,
//                columns:[col]
//            })
//        }

        // 采购单
//        var col = [
//            {title: '物资编码', field: 'wzgg.oldcode', width: 150},
////            {title: '物资编码(新)', field: 'wzgg.code', width: 150},
//            {title: '物资名称', field: 'wzgg.name', width: 150},
//            {title: '型号规格', field: 'wzgg.ggxh', width: 150},
//            {title: '采购单号', field: 'cgdh', width: 150},
//            {title: '开单日期', field: 'kdrq', width: 150},
//            {title: '验收人', field: 'ysr', width: 150},
//            {title: '验收日期', field: 'yssj', width: 150},
//            {title: '入库人', field: 'rkr', width: 150},
//            {title: '入库日期', field: 'rkrq', width: 150}
//        ];
//        if (row) {
////            $("#bgjhgyz_cgd_grid").datagrid({
////                rownumbers: "true",
////                singleSelect: "true",
////                showFooter: true,
////                fitColumns: true,
////                pageNumber: 1,
////                url: $.webPath() + "/jhgl/bgjhlr/findCgdBybgjhId?typeId=3&id=" + row.id,
////
////                columns: [col]
////            });
//            $('#bgjhgyz_cgd_grid').datagrid({
//                pageNumber:1,
//                url: $.webPath() + "/jhgl/bgjhlr/findCgdBybgjhIddetail?typeId=3&id=" + row.id,
//                columns:[col]
//            })
//        }

        var col = [
            {title: '物资编码', field: 'wzgg.oldcode',align: 'center', width: 150},
//        {title: '物资编码(新)', field: 'wzgg.code', width: 150},
            {title: '物资名称', field: 'wzgg.name',align: 'center', width: 150},
            {title: '型号规格', field: 'wzgg.ggxh',align: 'center', width: 150},
            {title: '型号规格', field: 'wzgg.th',align: 'center', width: 150},
            {title: '单位', field: 'wzgg.dw',align: 'center', width: 60},
            {title: '需求计划数量', field: 'jhsl',align: 'right',
                formatter: function (value) {
                    var sl = formatSlForHtml(value, ",");
                    return sl;
                }, width: 80},
            {title: '本站库存数量', field: 'kcsl',align: 'right',
                formatter: function (value) {
                    var sl = formatSlForHtml(value, ",");
                    return sl;
                }, width: 80},
            {title: '一级平衡数量', field: 'sl1',align: 'right',
                formatter: function (value) {
                    var sl = formatSlForHtml(value, ",");
                    return sl;
                }, width: 80},
            {title: '二级平衡数量', field: 'sl2', align: 'right',width: 80},
            {title: '上报计划数量', field: 'sbsl',align: 'right', width: 80},
            {title: '退回原因', field: 'thyy',align: 'center', width: 200},
            {title: '退回日期', field: 'thrq',align: 'center', width: 150},
            {title: '退回人', field: 'thr',align: 'center', width: 100},
            {title: '到货日期', field: 'dhrq',align: 'center', width: 150},
            {title: '牌号', field: 'ph', align: 'center',width: 150},
            {title: '设备材料', field: 'sbcl',align: 'center', formatter: function (value) {
                if (value == 'sb') {
                    return "设备";
                }
                if (value == "cl") {
                    return "材料";
                }
            }, width: 150},
            {title: '备注', field: 'bz',align: 'center', width: 150},
            {title: '计划员', field: 'createUser.xm',align: 'center', width: 100}
        ];
//    $(tabs[0]).panel({
//                href: $.webPath() + "/jhgl/wzjhgl/bgjh_detail?id=" + "hz="
//    });
        if (row) {
            $("#wzjhgl_thwzjh_grid").datagrid({
                rownumbers: "true",
                singleSelect: "true",
                showFooter: true,
                fitColumns: true,
                pageNumber: 1,
                url: $.webPath() + "/jhgl/bgjhgyz/bgjhgyzmxList?id=" + row.id + "&zt=10",
                columns: [col]
            });
        }
    }


//===========================================
//==============此页面用到的函数===================
//===========================================
    function showBgjhgyzDialog(title, href, actionUrl) {
        var win = $.window({
            title: title,
            width: 1024,
            height: 470,
            maximizable: true,
            cache: false,
            url: href,
            modal: true,
            onMaximize: function () {
                $(this).find("#bgjhgyz_bgjhhzmx_edatagrid").edatagrid("resize");

            },
            onRestore: function () {
                $(this).find("#bgjhgyz_bgjhhzmx_edatagrid").edatagrid("resize");
            },
            onComplete: function () {
                //计划处理或退回
                $(this).find('#bgjhgyz_bgjhhzmx_toolbar [name="jhcl"]').click(function () {
                    var rows = $(win).find('#bgjhgyz_bgjhhzmx_edatagrid').edatagrid('getChecked');
                    if (rows.length > 0) {
                        $.messager.confirm('提示', '是否确认处理?', function (r) {
                            if (r) {
                                //结束编辑列的编辑状态
                                $(win).mask();
                                $(win).find('#bgjhgyz_bgjhhzmx_edatagrid').edatagrid("saveRow");
                                var arrayPrefix = "bgjhhzmxList";
                                var paramString = "{"
                                //物资计划明细
                                for (var i = 0; i < rows.length; i++) {
                                    var objectString = $.toJSONStringPrefix(rows[i], arrayPrefix + "[" + i + "].");
                                    paramString = paramString + objectString.substring(1, objectString.length - 1) + ",";
                                }
                                paramString = paramString + "\"bgjhhz.id\":" + $(win).find("#bgjhgyz_content_bgjhhz_id").val() + "}";
                                var param = $.parseJSON(paramString);
                                $.post($.webPath() + "/jhgl/bgjhgyz/gyzcl", param, function (data) {
                                    $(win).mask('hide');
                                    if (data.statusCode == 200) {
                                        reloadBgjhgyzGrid();
                                        $(win).find('#bgjhgyz_bgjhhzmx_edatagrid').edatagrid("reload");
                                        $.messager.show({
                                            title: '提示',
                                            msg: data.message,
                                            timeout: 3000,
                                            showType: 'slide'
                                        });
                                    } else {
                                        $.messager.alert('提示', data.message, 'error');
                                    }
                                });
                            }
                        });
                    } else {
                        $.messager.alert("提示", "请选择要处理的计划!", "waring");
                    }
                });
            }
        });
    }

//刷新表格
    function reloadBgjhgyzGrid() {
        $('#bgjhgyz_grid').datagrid('reload', {
            type: $("#bgjhgyz_toolbar [name='type']").val(),
            jhdwId: $("#bgjhgyz_toolbar [name='jhdwId']").val(),
            jhnf: $("#bgjhgyz_toolbar [name='jhnf']").val(),
            jhyf: $("#bgjhgyz_toolbar [name='jhyf']").val(),
            wzbm: $("#bgjhgyz_toolbar [name='wzbm']").val(),
            startDate: $("#bgjhgyz_toolbar [name='startDate']").val(),
            endDate: $("#bgjhgyz_toolbar [name='endDate']").val(),
            sm: $("#bgjhgyz_toolbar [name='sm']").val(),
            gyzsbcl: $("#gyzsbcl").combobox("getValue")
        });
    }

    $('#wzjhgl_bgthwzjh_grid').datagrid('reload', {
        type: $("#bgjhgyz_toolbar [name='type']").val(),
        jhdwId: $("#bgjhgyz_toolbar [name='jhdwId']").val(),
        jhnf: $("#bgjhgyz_toolbar [name='jhnf']").val(),
        jhyf: $("#bgjhgyz_toolbar [name='jhyf']").val(),
        wzbm: $("#bgjhgyz_toolbar [name='wzbm']").val(),
        sm: $("#bgjhgyz_toolbar [name='sm']").val()
    });

})

function formatHtDetail (value, row, index) {
    if (value)
        return "<a href='javascript:void(0);' style='color: red' onclick='viewGyzHtDetail(" + row.htid + ")' >" + value + "</a>"
    else
        return value;
}

function formatCgdDetail (value, row, index) {
    if (value)
        return "<a href='javascript:void(0);' style='color: red' onclick='viewGyzCgdDetail(" + row.cgdid + ")' >" + value + "</a>"
    else
        return value;
}

function viewGyzHtDetail(id) {
    $.window({
        title: '详细信息',
        iconCls: 'icon-tip',
        url: $.webPath() + "/htgl/cghtgl/cghtgl_detail?cghtId=" + id,
        width: 1050,
        height: 500,
        modal: true,
        isMax: false
    })
}

function viewGyzCgdDetail(id) {
    $.window({
        title: '详细信息',
        iconCls: 'icon-tip',
        url: $.webPath() + "/cggl/cgdgl/cgdgl_detail?cgd.id=" + id,
        width: 1050,
        height: 500,
        modal: true,
        isMax: false
    })
}
